package com.example.demo.controller;

import com.example.demo.dao.Blog_diary;
import com.example.demo.service.impl.Blog_diaryserviceimpl;
import com.example.demo.utils.CommonRetumType;
import com.example.demo.utils.error.BusinessException;
import com.example.demo.utils.error.EmBusinessError;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("diary")
public class Blog_diaryController extends BaseController{
    @Autowired
    Blog_diaryserviceimpl blog_diaryserviceimpl;

    @GetMapping()
    public CommonRetumType create(Blog_diary blog_diary) throws BusinessException {
        Integer diaryinsert = blog_diaryserviceimpl.insert(blog_diary);
        if (diaryinsert != null)
        {
            return CommonRetumType.create(diaryinsert);
        }else {
            throw new BusinessException(EmBusinessError.PARAMETER_INSERT_ERROR);
        }
    }

    @DeleteMapping()
    public CommonRetumType delete(@RequestParam("id") Integer id) throws BusinessException {
        Integer diarydelete = blog_diaryserviceimpl.deleteByPrimaryKey(id);
        if (diarydelete != null)
        {
            return CommonRetumType.create(diarydelete);
        }else {
            throw new BusinessException(EmBusinessError.PARAMETER_DELETE_ERROR);
        }
    }

    @PutMapping()
    public CommonRetumType update(Blog_diary blog_diary) throws BusinessException {
        Integer diaryupdate = blog_diaryserviceimpl.updateByPrimaryKey(blog_diary);
        if (diaryupdate != null)
        {
            return CommonRetumType.create(diaryupdate);
        }else {
            throw new BusinessException(EmBusinessError.PARAMETER_UPDATE_ERROR);
        }
    }

    @GetMapping("/findbyid{id}")
    public CommonRetumType selectbyid(@RequestParam("id") Integer id) throws BusinessException {
        Blog_diary diarybyid = blog_diaryserviceimpl.selectByPrimaryKey(id);
        if (diarybyid!=null)
        {
            return CommonRetumType.create(diarybyid);
        }else {
            throw new BusinessException(EmBusinessError.PARAMETER_SELECT_ERROR);
        }
    }

}
